<template>
  <el-dialog title="添加角色" :visible="visible" @close="hide(false)">
    <el-form
      label-width="100px"
      :model="form"
      :rules="rules"
      status-icon
      ref="form"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input autocomplete="off" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          autocomplete="off"
          type="textarea"
          :rows="5"
          v-model="form.description"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input autocomplete="off" v-model.number="form.sort"></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-radio v-model="form.status" :label="1">是</el-radio>
        <el-radio v-model="form.status" :label="0">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="hide(false)">取 消</el-button>
      <el-button @click="submit" type="primary">确 定</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import { createRole } from '@/request/api'
export default {
  props: {
    visible: {
      type: Boolean, default: false
    }
  },
  data() {
    return {
      form: {
        name: '',
        description: '',
        sort: 0,
        status: 1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '角色排序必须是一个数字', trigger: 'blur' }]
      }
    }
  },
  methods: {
    hide(isReload) {
      this.$emit('show', isReload)
    },
    submit() {
      createRole(this.form).then(res => {
        if (res.code === 200) {
          this.$message.success('添加成功')
          this.hide(true)
        }
      })
    }
  }
}
</script>
 
<style lang = "less" scoped>
</style>